package com.china08.yxyapi.api.manage.tec.office.classnotice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass.YxyNotifictionUser;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 老师 班级通知 新增
 * 
 */
@CrossOrigin
@RestController("TecClassnoticeCrt2Controller")
public class ClassnoticeCrt2Controller extends YxyApiBaseController {

    @Autowired
    private YxyNoticeClassRepository yxyNoticeClassRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/classnotice/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        RespModel respModel = new RespModel();
        YxyProfile yxyProfile = getProfile(request, response);
        String currentUserId = yxyProfile.getId();
        String schoolId = yxyProfile.getSchoolId();
        String userNick = yxyProfile.getUserNick();
        String avatar = yxyProfile.getUserAvator();

        // 班级通知列表
        List<YxyNoticeClass> yxyNoticeClassLst = new ArrayList<YxyNoticeClass>();

        // 接收人列表
        List<YxyNotifictionUser> receiver = null;
        // 一个接收人对象
        YxyNotifictionUser yxyNotifictionUser = null;
        // 班级id列表
        List<String> classIdLst = reqModel.classIdLst;
        // 班级名称列表
        List<String> classNameLst = reqModel.classNameLst;
        // 一个班级学生的集合信息
        Map<String, List<Map<String, String>>> oneclassStudentInfoMap = null;
        // 一个班级学生的列表
        List<Map<String, String>> oneclassStudentLst = null;

        YxyNoticeClass yxyNoticeClass = null;
        for (int i = 0; i < classIdLst.size(); i++) {
            receiver = new ArrayList<YxyNotifictionUser>();
            yxyNoticeClass = new YxyNoticeClass();
            yxyNoticeClass.setSchoolId(schoolId);
            yxyNoticeClass.setTitle(reqModel.title);
            yxyNoticeClass.setContent(reqModel.content);
            yxyNoticeClass.setAttachmentName(reqModel.attachmentName);
            yxyNoticeClass.setAttachmentPath(reqModel.attachmentPath);

            yxyNoticeClass.setClassId(classIdLst.get(i));
            yxyNoticeClass.setSchoolId(schoolId);
            // 查询班级名称
            yxyNoticeClass.setClassNick(classNameLst.get(i));

            yxyNoticeClass.setImage(reqModel.image);
            yxyNoticeClass.setUserId(currentUserId);
            yxyNoticeClass.setNickName(userNick);
            yxyNoticeClass.setAvatar(avatar);

            // 班级内的所有的学生信息
            oneclassStudentInfoMap = reqModel.classStudentInfoLst.get(i);
            // 根据主键班级id 获取班级内的所有学生信息
            oneclassStudentLst = oneclassStudentInfoMap.get(classIdLst.get(i));
            // 遍历所有学生 并存入该班级通知接受人字段中
            for (Map<String, String> map2 : oneclassStudentLst) {
                yxyNotifictionUser = new YxyNoticeClass().new YxyNotifictionUser();
                yxyNotifictionUser.setUserId(map2.get("userId"));
                yxyNotifictionUser.setUserNick(map2.get("userNick"));
                yxyNotifictionUser.setIsRead(0);
                receiver.add(yxyNotifictionUser);
            }
            yxyNoticeClass.setReceiver(receiver);
            yxyNoticeClassLst.add(yxyNoticeClass);
        }
        // FIXED 业务库
        yxyNoticeClassRepository.save(yxyNoticeClassLst);
        respModel.setMessage("保存成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        // 班级id list
        public List<String> classIdLst;
        // 班级name list
        public List<String> classNameLst;
        // 班级学生列表
        public List<Map<String, List<Map<String, String>>>> classStudentInfoLst;
        // 标题
        public String title;
        // 内容
        public String content;
        // 图片列表
        public List<String> image;
        // 附件名
        public String attachmentName;
        // 附件路径
        public String attachmentPath;

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
